from django.db import models
from users.models import UserProfile
import datetime


equit_stats_choice = (
    ('used','使用'),
    ('freed', '空闲'),
    ('delete', '下架')
)

equit_type_choice = (
    ('kvm','KVM'),
    ('xen', 'XEN'),
    ('docker', 'Docker'),
    ('physcial', '物理机'),
    ('router', 'Router'),
    ('switch', '交换机')
)

class IDC(models.Model):
    name = models.CharField(verbose_name='机房名称', max_length=128)
    address = models.CharField(verbose_name='地址', max_length=128)
    contact_user = models.CharField(verbose_name='联系人', max_length=12)
    
    mobile = models.CharField(verbose_name='联系电话', max_length=12)
    other_mobile =  models.CharField(verbose_name='备用电话', max_length=12, blank=True)
    create_time = models.DateTimeField(verbose_name='上架时间', default=datetime.datetime.now)
    remark = models.TextField(verbose_name='操作系统')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name = '机房'
        verbose_name_plural = verbose_name


class Assets(models.Model):
    hostname = models.CharField(verbose_name='主机名', max_length=256)
    inner_ip = models.GenericIPAddressField(verbose_name='IP')
    outer_ip = models.GenericIPAddressField(verbose_name='外网IP', blank=True, null=True)
    
    cpu_info = models.CharField(verbose_name='CPU信息', max_length=256) # core * 4
    memory = models.IntegerField(verbose_name='内存信息') # MB
    os_info = models.CharField(verbose_name='操作系统', max_length=128)
    disk_size = models.IntegerField(verbose_name='内盘容量', default=0) # default MB
    nic_info = models.CharField(verbose_name='网卡信息', max_length=128) # [xxx, xxx]
    sn = models.CharField(verbose_name='设备编号', max_length=128)
    equip_type = models.CharField(verbose_name='设备类型', choices=equit_type_choice, max_length=12)
    equip_stats = models.CharField(verbose_name='设备状态', choices=equit_stats_choice, max_length=12)
    is_active = models.BooleanField(verbose_name='状态')
    create_time = models.DateTimeField(verbose_name='上架时间', default=datetime.datetime.now)

    projects = models.ManyToManyField('Projects')
    appservs = models.ManyToManyField('AppServ')
    idc = models.ForeignKey(IDC, verbose_name='机房')
    
    remark = models.TextField(verbose_name='操作系统')

    def __unicode__(self):
        return self.hostname

    class Meta:
        verbose_name = '设备'
        verbose_name_plural = verbose_name


class Projects(models.Model):
    name = models.CharField(verbose_name='名称', max_length=32)
    main_ops = models.ForeignKey(UserProfile, verbose_name='运维负责人')
    back_ops = models.CharField(verbose_name='第二负责人', max_length=32)
    dev_user = models.CharField(verbose_name='研发人员', max_length=32) # 存储Id
    create_time = models.DateTimeField(verbose_name='上架时间', default=datetime.datetime.now)
    remark = models.TextField(verbose_name='备注')
    
    
    def get_dev_users(self):
        pass
    
    def seconds_ops(self):
        pass
    
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '项目'
        verbose_name_plural = verbose_name


class AppServ(models.Model):
    
    name = models.CharField(verbose_name='名称', max_length=32)
    cmd = models.CharField(verbose_name='操作命令', max_length=64) # /etc/init.d/sshd  {start| stop| restart|reload}
    ports = models.IntegerField(verbose_name='端口') # TCP 主要对外服务端口
    create_time = models.DateTimeField(verbose_name='上架时间', default=datetime.datetime.now)
    remark = models.TextField(verbose_name='备注')
    
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '服务'
        verbose_name_plural = verbose_name

